﻿using Game2DFramework;
using Game2DFramework.Drawing.Particles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.Game.Particles
{
    class RocketProjectile : Projectile
    {
        private readonly ExplosionManager _explosionManager;
        private readonly DefaultEmitter _emitter;
        private readonly SpriteParticleSystem _particleSystem;

        public RocketProjectile(Game2D game, Vector2 position, float speed, Vector2 direction, int damage) : base(game, position, speed, direction, damage)
        {
            _explosionManager = Game.GetGlobalObject<ExplosionManager>("ExplosionManager");
            _emitter = new DefaultEmitter {Angle = Rotation, LifeTime = 1.5f, Speed = 0, SpawnTime = 0.1f};
            _emitter.SetAlpha(1.0f, 0.0f);
            _emitter.SetScaleRange(1.0f, 4.0f);
            _particleSystem = new SpriteParticleSystem(_emitter, Game.Content.Load<Texture2D>("textures/smoke_particle")) { IsEnabled = true };
        }

        public override void Update(float elapsed)
        {
            base.Update(elapsed);
            _particleSystem.IsEnabled = !Deleted;
            _emitter.SpawnPosition = Position - Direction * 16;
            _particleSystem.Update(elapsed);
        }

        public override bool ReadyToBeRemoved
        {
            get
            {
                return base.ReadyToBeRemoved && !_particleSystem.HasActiveParticles;
            }
        }

        public override void OnCollide(GameObject other)
        {
            if (Deleted) return;
            base.OnCollide(other);
            _explosionManager.SpawnExplosion(Position);
        }

        public override void Draw()
        {
            base.Draw();
            _particleSystem.Draw(Game.SpriteBatch);
        }
    }
}
